package mapper;


import java.util.Date;

//import java.util.Date;

import com.jfinal.plugin.activerecord.Db;

import models.Profile;
import models.User;

public class UserMapper {
	
	String GET_USER_SQL = "select * from User where name = ? ;";
//	String CREAT_USER_SQL = "INSERT INTO User [(id, name, password, phoneNumber, createdDate)] VALUES "
//			+ "(?, ?, ?) ;";
//	String UPDATE_PWD_SQL = "";
	
	String GET_PROFILE_SQL = "select * from Profile where userId = ? ;";
//	String CREATE_PROFILE_SQL = "INSERT INFO PROFILE [(id, userId, nickName, phoneNumber, birthDate, emailAddress, createdDate)] VALUES "
//			+ "(?, ?, ?, ?, ?, ?, ?) ;";
	
//	String UPDATEDDATE = "updatedDate";

	public User getUserByName(String userName){
		return User.dao.findFirst(GET_USER_SQL, userName);
//		Record record = Db.findFirst(GET_USER_SQL, userName);
//		return new User().put(record);
	}

	public User getUserById(String userId){
		return User.dao.findById(userId);
//		Record record = Db.findById("User", userId);
//		return new User().put(record);
	}

	public boolean userSignUp(User user,Profile profile){
		return Db.tx(() -> {
			user.setCreatedDate(new Date());
//			Db.save("User", user.toRecord());
			user.save();
			profile.setUserId(user.getId());
			profile.setCreatedDate(new Date());
//			Db.save("Profile", profile.toRecord());
			profile.save();
			return true;
		});
	}
	
	public boolean updateUser(User newUser){
//		newUser.setUpdatedDate(new Date());
//		return Db.update("User", newUser.toRecord());
		return newUser.update();
	}
	
	public Profile getProfile(String userId){
		return Profile.dao.findFirst(GET_PROFILE_SQL, userId);
//		Record record = Db.findFirst(GET_PROFILE_SQL, userId);
//		return new Profile().put(record);
		
	}
	
//	public Record getProfileRecord(String userId){
//		return Db.findFirst(GET_PROFILE_SQL, userId);
//	}
	
	public boolean updateProfile(Profile newProfile){
		// using trigger instead of it
//		newProfile.setUpdatedDate(new Date());
//		return Db.update("Profile", newProfile.toRecord());
		return newProfile.update();
	}

}
